package com.example.newsclient.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.newsclient.DetailedActivity;
import com.example.newsclient.R;
import com.example.newsclient.bean.Top;
import com.example.newsclient.bean.WeChat;

import java.util.List;

/**
 * Created by 袁永杰 on 2019/5/30.
 * 微信精选列表适配器
 */

public class MyRecyclerAdapter2 extends RecyclerView.Adapter<MyRecyclerAdapter2.MyViewHolder> implements View.OnClickListener{

    private List<WeChat.ResultBean.ListBean> listBeanList;
    private Context context;
    private RecyclerView recyclerView;

    public MyRecyclerAdapter2(List<WeChat.ResultBean.ListBean> listBeanList, Context context){
        this.listBeanList = listBeanList;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.wechat_item,null);
        MyViewHolder myViewHolder = new MyViewHolder(itemView);
        itemView.setOnClickListener(this);
        recyclerView = (RecyclerView) parent;
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        WeChat.ResultBean.ListBean listBean = listBeanList.get(position);
        holder.tv_news_title.setText(listBean.getTitle());
        holder.tv_author.setText(listBean.getSource());
    }

    @Override
    public int getItemCount() {
        return listBeanList.size();
    }

    /**
     * 注册点击事件
     * @param view
     */
    @Override
    public void onClick(View view) {
        int position = recyclerView.getChildLayoutPosition(view);
        //点击相应的子项，显示详细新闻信息
        Intent intent = new Intent(context, DetailedActivity.class);
        intent.putExtra("news_url",listBeanList.get(position).getUrl());
        context.startActivity(intent);
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView tv_news_title;
        TextView tv_author;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_news_title = itemView.findViewById(R.id.tv_news_title);
            tv_author = itemView.findViewById(R.id.tv_author);
        }
    }
}
